import tool from '@/common/tool'

function syncData(list, index, data, obj) {
  let reviewerDesc = []
  obj && obj.forEach(item => {
    reviewerDesc.push(item.label)
  })
  data.reviewerDesc = reviewerDesc
  if (index >= 0) list.splice(index, 1, data)
}

function RemoveSameItem(listOne, listTwo, key) {
  let newList = []
  loop: for (let itemOne of listOne) {
      for (let itemTwo of listTwo) {
          if (itemOne[key] == itemTwo[key]) {
              continue loop
          }
      }
      newList.push(itemOne)
  }
  return newList.concat(listTwo)
}

function BusinessOrganflatten(organArr, checkArr) {
  let BusinessOrganflattenArr = []
  let organTmpArr = tool.flattenArr(organArr)
  organTmpArr && organTmpArr.forEach(item => {
    let obj = {}
    if (checkArr.includes(item.id)) {
      obj.companyId = item.id
      obj.companyDesc = item.title
      BusinessOrganflattenArr.push(obj)
    }
  })
  return BusinessOrganflattenArr
}

//处理表格数据=>接口需要的
function formatProcessDataList(data) {
  let tmpArr = []
  data && data.forEach(item => {
    let obj = {}
    obj.node = item._index + 1
    obj.reviewerId = item.reviewerId
    obj.reviewerType = item.reviewerType
    obj.reviewerDesc = item.reviewerId && item.reviewerDesc.join('、')
    tmpArr.push(obj)
  })
  return tmpArr
}

export default {
  syncData,
  BusinessOrganflatten,
  formatProcessDataList,
  RemoveSameItem
}